﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Kernel;

namespace SppdProjectHelper
{
    public partial class FormMain_nor : FormBase,IControlEx
    {
        public FormMain_nor()
        {
            InitializeComponent();

            this.tableLayoutPanel_main.Controls.Add(_cv, 0, 2);
            this.tableLayoutPanel_center.Controls.Add(_pfv, 2, 0);

            this.AddExControl(this);
            this.AddExControl(_cv);
            this.AddExControl(_pfv);

            this.LoadAllExControls();
            SysMisc.LogHelper.LogUpdateEvent += LogHelper_LogUpdateEvent;

            /*尝试打开当前DWG文档下的SPM文档*/
            funcTryToOpenPrjFileOfActDoc();
        }

        private void LogHelper_LogUpdateEvent(SysMisc.LogHelper.LogType typ, Type t, string log)
        {
            Misc.WriteToTxtFile(log+"\r\n");
        }

        private Project _prj = new Project();
        private CategoryViewer _cv = new CategoryViewer();
        private PrjFileViewer _pfv = new PrjFileViewer();

        #region 实现IControlEx接口
        void IControlEx.Init() { }
        void IControlEx.Load() { UI_Load(); }
        void IControlEx.Update() { UI_Update(); }
        void IControlEx.Save() { Save(); }
        bool IControlEx.Updated { get; }
        #endregion

        public void UI_Load()
        {

        }

        public void UI_Update()
        {
            funcUpdateDataToUI();

            //this.UpdateAllExControls();
        }

        public void Save()
        {
            funcUpdateDataFromUI();
        }

        private void funcUpdateDataToUI()
        {
            ProjectConfig prj_cfg = _prj.ProjectConfig;
            {//主配置
                this.textBox_cmpyCode.Text = prj_cfg.MainCfg.CmpyCode;
                this.textBox_prjCode.Text = prj_cfg.MainCfg.PrjCode;
                this.textBox_prjName.Text = prj_cfg.MainCfg.PrjName;
                this.textBox_subject.Text = prj_cfg.MainCfg.Subject;
                this.textBox_state.Text = prj_cfg.MainCfg.State;
                this.textBox_volName.Text = prj_cfg.MainCfg.VolName;
                this.textBox_volNum.Text = prj_cfg.MainCfg.VolNum.ToString();
                this.textBox_rowNum.Text = prj_cfg.MainCfg.RowNum.ToString();
                this.textBox_pubDate.Text = prj_cfg.MainCfg.PubDate;
                this.textBox_startNum.Text = prj_cfg.MainCfg.StartNum.ToString();
            }
            {//打印配置
                this.textBox_printerName.Text = prj_cfg.MainPrint.PrinterName;
                this.textBox_copyNum.Text = prj_cfg.MainPrint.CopyNum.ToString();
                this.checkBox_byCollate.Checked = prj_cfg.MainPrint.ByCollate;
                this.checkBox_byInverse.Checked = prj_cfg.MainPrint.ByInverse;
                this.checkBox_bySize.Checked = prj_cfg.MainPrint.BySize;

            }
        }

        private void funcUpdateDataFromUI()
        {
            ProjectConfig prj_cfg = _prj.ProjectConfig;
            {//主配置
                prj_cfg.MainCfg.CmpyCode = this.textBox_cmpyCode.Text;
                prj_cfg.MainCfg.PrjCode = this.textBox_prjCode.Text ;
                prj_cfg.MainCfg.PrjName = this.textBox_prjName.Text ;
                prj_cfg.MainCfg.Subject = this.textBox_subject.Text ;
                prj_cfg.MainCfg.State = this.textBox_state.Text ;
                prj_cfg.MainCfg.VolName = this.textBox_volName.Text ;
                prj_cfg.MainCfg.VolNum = Convert.ToInt32(this.textBox_volNum.Text );
                prj_cfg.MainCfg.RowNum = Convert.ToInt32(this.textBox_rowNum.Text);
                prj_cfg.MainCfg.PubDate = this.textBox_pubDate.Text ;
                prj_cfg.MainCfg.StartNum = Convert.ToInt32(this.textBox_startNum.Text);
            }
            {//打印配置
                prj_cfg.MainPrint.PrinterName = this.textBox_printerName.Text;
                prj_cfg.MainPrint.CopyNum = Convert.ToInt32(this.textBox_copyNum.Text);
                prj_cfg.MainPrint.ByCollate = this.checkBox_byCollate.Checked;
                prj_cfg.MainPrint.ByInverse = this.checkBox_byInverse.Checked;
                prj_cfg.MainPrint.BySize = this.checkBox_bySize.Checked;

            }
        }

        private void toolStripButton_Click(Object sender, EventArgs ex)
        {
            ToolStripButton tsb = (ToolStripButton)sender;
            switch (tsb.Text)
            {
                case "打开工程":
                    {
                        OpenFileDialog ofd = new OpenFileDialog();
                        ofd.Filter = "spm文件(*.spm) | *.spm";//文件类型
                        DialogResult dr = ofd.ShowDialog();
                        if( dr == DialogResult.OK)
                        {
                            //_prj.ProjectConfig = SysMisc.Serialization.DeSerializeFromXml<ProjectConfig>(ofd.FileName);

                            this._prj = new Project(ofd.FileName);
                            this._pfv.Project = this._prj;
                            this._cv.Project = this._prj;
                            this.UpdateAllExControls();
                        }
                    }
                    break;
                case "保存工程":
                    {
                        this.SaveAllExControls();
                        if( false == _prj.Save())
                        {
                            Misc.Alert(_prj.ErrorText);
                        }else
                        {
                            Misc.Alert("保存成功");
                        }
                    }
                    break;
            }
        }

        private void Button_Click(Object sender, EventArgs ex)
        {
            Button btn = (Button)sender;
            switch (btn.Text)
            {
                case "确认修改":
                    {
                        funcUpdateDataFromUI();
                        Misc.Alert("保存成功！");

                    }
                    break;
                case "开始打印":
                    {
                        Autodesk.AutoCAD.PlottingServices.PlotProgressDialog ppd = new Autodesk.AutoCAD.PlottingServices.PlotProgressDialog(false,1,true);
                        ppd.OnBeginPlot();
                    }
                    break;
            }
        }

        /// <summary>
        /// 尝试打开当前活动文档下的工程文件
        /// </summary>
        private void funcTryToOpenPrjFileOfActDoc()
        {
            //string filName = null;
            //DirectoryInfo di = new DirectoryInfo( Common.GetCurrentActiveDocPath() );
            //FileInfo[] fis = di.GetFiles();
            //foreach( FileInfo fi in fis )
            //{
            //    if( fi.Extension.Equals(".spm"))
            //    {
            //        filName = fi.FullName;
            //    }
            //}
            //if( null != filName )
            //{
            //    _prj_cfg = SysMisc.Serialization.DeSerializeFromXml<ProjectConfig>(filName);

            //    this._pfv.ProjectConfig = _prj_cfg;
            //    this._cv.ProjectConfig = _prj_cfg;
            //    this.UpdateAllExControls();
            //}

            if( true == _prj.TryToOpenPrjFileOfActDoc())
            {
                this._pfv.Project = _prj;
                this._cv.Project = _prj;
                this.UpdateAllExControls();
            }
        }
    }
}
